'use strict';
const crypto = require("crypto");
const jwt = require("jwt-simple");
const {tokenExp} = require('const-data');
const encryptPassword = require('encrypt-password');
const db = uniCloud.database();

exports.main = async (event, context) => {
	let {
		email,
		password,
	} = event

	let userInfo = {
		email
	}

	let userInDB = await db.collection('users').where({
		email,
		password: encryptPassword(password)
	}).get()

	let tokenSecret = crypto.randomBytes(16).toString('hex'), //token
		token = jwt.encode(userInfo, tokenSecret);

	let userUpdateResult;
	if (userInDB.data && userInDB.data.length === 0) {
		return {
			status: -1,
			msg: '用户名或密码不正确'
		}
	} else {

		//登录成功,更新token
		userUpdateResult = await db.collection('users').doc(userInDB.data[0]._id).update({
			tokenSecret,
			exp: Date.now() + tokenExp
		})
	}

	if (userUpdateResult.id || userUpdateResult.affectedDocs === 1) {
		return {
			status: 1,
			token,
			relevanceUserID:userInDB.data[0].relevanceUserID,
			msg: "登录成功"
		}
	}

	return {
		status: -1,
		msg: '登录失败'
	}
};
